
#ifndef MYOPENGLWIDGET_H
#define MYOPENGLWIDGET_H


#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
#include <QElapsedTimer>
#include <QTimer>
#include <QWheelEvent>
#include "camera.h"
#include <QOpenGLTexture>
#include <QOpenGLFunctions>
#include "model.h"
#include <iostream>
#include <QFile>
#include <QMutex>
#include <QMutexLocker>

using namespace std;

struct ModelInfo{
    Model *model;
    QVector3D worldPos;
    float pitch;
    float yaw;
    float roll;
    bool isSelected;
    QString name;
};

class MyOpenglWidget : public QOpenGLWidget,QOpenGLFunctions
{
    Q_OBJECT
public:
    explicit MyOpenglWidget(QWidget *parent = nullptr);
    ~MyOpenglWidget();
    void loadModel(string path);
    void setupMesh(const Mesh &mesh, unsigned int &VAO, unsigned int &VBO, unsigned int &EBO);
    Mesh *processMesh(float *vertices, int size, unsigned int textureId);

     QFile file;
     unsigned int VBO, VAO;
    // QOpenGLWidget interface
     QMutex line_mutex;
     QMutex model_mutex;
protected:
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;

    void init_line();
public slots:
    void goAhead();
    void on_timeOut();
private:
    QTimer m_timer;
    QElapsedTimer m_time;
    QOpenGLShaderProgram m_shaderProgram;
    QOpenGLShaderProgram m_lightShaderProgram;
    QOpenGLShaderProgram line_shaderProgram;
    Camera m_camera;
    QVector3D getCameraInitPos(float min,float max);
    QMap<QString, ModelInfo> m_Models;
    Mesh * m_PlaneMesh;
    Mesh * m_LineMesh;
    // QWidget interface

    QPoint getPoint(QPointF A,QPointF B,QPointF C, float t);
protected:
    void mouseMoveEvent(QMouseEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;
    void keyPressEvent(QKeyEvent *event) override;
};

#endif // MYOPENGLWIDGET_H
